﻿using System;
using System.Diagnostics;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms;
using SystemsAdmin.Properties;
using Spring.Context;
using Spring.Context.Support;

namespace SystemsAdmin
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class SystemsAdminWindow : Window
    {
        private List<Computer> _computers;
        ExternalApp _external_app;

        public SystemsAdminWindow()
        {
            LoadComputersInfo();
            InitializeComponent();
            ComputersList.DataContext = _computers;
        }

        private void LoadComputersInfo()
        {

            IApplicationContext context = new XmlApplicationContext("configuration\\applicationContext.XML");
            ComputerRepository computer_repository = (ComputerRepository)context.GetObject("computer_repository");
            _external_app = (ExternalApp)context.GetObject("external_app");

            _computers = computer_repository.GetAllComputers();


        }



        void RunWithRedirect(string cmdPath, string args)
        {
            var proc = new Process();
            Process p = new Process();
            String s;
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.FileName = cmdPath;
            p.StartInfo.Arguments = args;
            p.Start();
            StreamReader reader = p.StandardOutput;
            //StreamWriter writer = p.StandardInput;
            //writer.AutoFlush = true;
            //writer.Write("ls\n");
            //writer.Write("exit\n");
            s = reader.ReadToEnd();

            txt_output.Text += s;
        }

        void proc_DataReceived(object sender, DataReceivedEventArgs e)
        {
            if (e.Data != null)
            {
                txt_output.Text += e.Data.ToString();
            }
        }

        private void ExecWolButton_Click(object sender, RoutedEventArgs e)
        {
            //Get all the computers selected
            foreach (var selected_computer in ComputersList.SelectedItems)
            {
                ((Computer)selected_computer).TurnOn();
                txt_output.Text += "\n Sent WOL command to " + selected_computer;
            }
        }

        private void PingBtn_Click(object sender, RoutedEventArgs e)
        {
            List<string> ips_selected = GetIPsSelected();
            foreach (string ip in ips_selected)
            {
                string executable = Environment.ExpandEnvironmentVariables(@"%SystemRoot%\system32\ping.exe");
                RunWithRedirect(executable, ip);
            }
        }

        private List<string> GetIPsSelected()
        {
            List<string> ips_selected = new List<string>();
            //Get all the computers selected
            foreach (var selected_computer in ComputersList.SelectedItems)
            {
                ips_selected.Add(((Computer)selected_computer).IPAddress);
            }
            return ips_selected;
        }
        private List<string> GetMACsSelected()
        {
            List<string> macs_selected = new List<string>();
            //Get all the computers selected
            foreach (var selected_computer in ComputersList.SelectedItems)
            {
                macs_selected.Add(((Computer)selected_computer).MACAddress);
            }
            return macs_selected;
        }

        private void DoRDP_Click(object sender, RoutedEventArgs e)
        {
            foreach (var selected_computer in ComputersList.SelectedItems)
            {
                _external_app.StartRemoteDesktop((Computer)selected_computer);
            }


        }

        private void BrowseSourceFileBtn_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            System.Windows.Forms.DialogResult b = ofd.ShowDialog();
            if (b == System.Windows.Forms.DialogResult.OK)
            {
                CopySourceFiles.Text = ofd.FileName;
            }
        }

        private void CopyFiles_Click(object sender, RoutedEventArgs e)
        {
            string source = CopySourceFiles.Text;
            string destination = CopyTargetPath.Text;
            foreach (var selected_computer in ComputersList.SelectedItems)
            {
                string  copy_result = ((Computer)selected_computer).CopyFiles(source, destination);
                if (copy_result != Computer.FILES_COPIED_OK_MSG)
                    System.Windows.MessageBox.Show(copy_result);
            }
        }

        private void RefreshBtn_Click(object sender, RoutedEventArgs e)
        {
            foreach (var selected_computer in ComputersList.Items)
            {
                ((Computer)selected_computer).Refresh();
            }
            ComputersList.DataContext = _computers;
        }

        private void CopyFolders_Click(object sender, RoutedEventArgs e)
        {
            string source = CopySourceFolders.Text;
            string destination = CopyFolderTargetPath.Text;
            foreach (var selected_computer in ComputersList.SelectedItems)
            {
                string copy_result = ((Computer)selected_computer).CopyFolder(source, destination);
                if (copy_result != Computer.FILES_COPIED_OK_MSG)
                    System.Windows.MessageBox.Show(copy_result);
            }

        }

        private void BrowseSourceFolderBtn_Click(object sender, RoutedEventArgs e)
        {
            FolderBrowserDialog ofd = new FolderBrowserDialog();
            System.Windows.Forms.DialogResult b = ofd.ShowDialog();
            if (b == System.Windows.Forms.DialogResult.OK)
            {
                CopySourceFolders.Text = ofd.SelectedPath;
            }

        }

        private void PsExecBtn_Click(object sender, RoutedEventArgs e)
        {

            foreach (var selected_computer in ComputersList.SelectedItems)
            {
                txt_output.Text += _external_app.ExecuteCommand((Computer)selected_computer,
                 PsExecCommandTxt.Text,
                PsExecUser.Text, PsExecPassword.Password);
            }
        }

    }
}
